package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @PackageName:com.springboot.reggie.controller
 * @ClassName:UserCtroller
 * @Description: TODO
 * @Author: U87
 * @Date:2022/4/29 15:11
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserCtroller {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate ;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpServletRequest request) {
        String phone = user.getPhone();
        if (phone != null) {
            //生成验证码
            String code = ValidateCodeUtils.generateValidateCode(6).toString();
            //调用阿里云工具类发送

            //SMSUtils.sendMessage("签名名称", "模板code", phone, code);
            //本地测试 直接打印到控制台输出测试就好,不用走阿里云
            log.info("短信验证: {}", code);
            //存储验证码到session
            //request.getSession().setAttribute(phone, code);
            //存储验证码到redis
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("短信发送成功");
        }
        return R.error("短信发送失败");
    }

    @PostMapping("/login")
    public R<User> sendMsg(@RequestBody Map map, HttpServletRequest request) {
        String phone = (String) map.get("phone");
        String code = (String) map.get("code");
        //获取session中的code
        //Object codeInSession = request.getSession().getAttribute(phone);
        //获取缓存中的验证码
        Object codeInSession = redisTemplate.opsForValue().get(phone);
        //判断是否相等
        if (code != null && code.equals(codeInSession)) {
            //相等后判断该手机号有没有注册
            LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(User::getPhone, phone);
            User user = userService.getOne(userWrapper);
            if (user == null) {
                //判断当前手机号对应的用户是否为新用户，如果是新用户就自动完成注册
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            request.getSession().setAttribute("user", user.getId());
            //移除redis中的缓存
            redisTemplate.delete(phone);
            return R.success(user);

        }
        return R.error("登录失败");
    }

    @PostMapping("/loginout")
    public R<String> longinout(HttpServletRequest request) {
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

}
